bake
and stew
functions perform analogously:
an R computation is performed and stored in a named file.
If the function is called again and the file is present, the computation is not executed; rather, the results are loaded from the file in which they were previously stored.
Moreover, via their optional seed
argument, bake
and stew
can control the pseudorandom-number generator (RNG) for greater reproducibility.
After the computation is finished, these functions restore the pre-existing RNG state to avoid side effects. The freeze
function doesn't save results, but does set the RNG state to the specified value and restore it after the computation is complete.
bake(file, expr, seed, kind = NULL, normal.kind = NULL)
stew(file, expr, seed, kind = NULL, normal.kind = NULL)
freeze(expr, seed, kind = NULL, normal.kind = NULL)
bake
, this will contain a single R object and hence be an RDS file (extension rds);
for stew
, this will contain one or more named R objects and hence be an RDA file (extension rda).
set.seed
.
bake
returns the value of the evaluated expression expr
.
Other objects created in the evaluation of expr
are discarded along with the temporary, local environment created for the evaluation.The latter behavior differs from that of stew
, which returns the names of the objects created during the evaluation of expr
.
After stew
completes, these objects exist in the parent environment (that from which stew
was called).freeze
returns the value of evaluated expression expr
.
However, freeze
evaluates expr
within the parent environment, so other objects created in the evaluation of expr
will therefore exist after freeze
completes.
bake
and stew
first test to see whether file
exists.
If it does, bake
reads it using readRDS
and returns the resulting object.
By contrast, stew
loads the file using load
and copies the objects it contains into the user's workspace (or the environment of the call to stew
). If file
does not exist, then both bake
and stew
evaluate the expression expr
;
they differ in the results that they save.
bake
saves the value of the evaluated expression to file
as a single R object.
The name of that object is not saved.
By contrast, stew
creates a local environment within which expr
is evaluated;
all objects in that environment are saved (by name) in file
.
## Not run:
# bake(file="example1.rds",{
# x <- runif(1000)
# mean(x)
# })
#
# stew(file="example2.rda",{
# x <- runif(10)
# y <- rnorm(n=10,mean=3*x+5,sd=2)
# })
#
# plot(x,y)
# ## End(Not run)
freeze(runif(3),seed=5886730)
freeze(runif(3),seed=5886730)
Run the code above in your browser using DataLab